layui.use(['element', 'jquery', 'layer', 'laytpl','laypage','form','table'], function() {
	var $ = layui.jquery,
		layer = layui.layer,
		laytpl = layui.laytpl,
		laypage=layui.laypage,
		form=layui.form,
		element = layui.element,
		table = layui.table;

		//从url路径中获取商品ID
		var str;
		var bookId;
		//获取userId
		var userId ;
		var b;//未收藏为true,已收藏为false
	//先查看有没有已经收藏了的
	$(document).ready(function () {
		 str=window.location.pathname;
		 bookId=str.substring(str.lastIndexOf("/")+1);
		 console.log(bookId);
		//获取userId
		 userId = document.getElementById("userId").value;
		console.log(userId);
		 b = true;//未收藏为true,已收藏为false
		$.getJSON('/fav/selectFav',"userId="+userId,function (res) {
			console.log(res);
			if(res.data==''){//检测返回值是否为空
				$("#takeFavBtn").html("加入收藏夹");
			}
			for(var i =0;i<res.data.length;i++){
				if(bookId==res.data[i].bookId){
					$("#takeFavBtn").html("取消收藏");
					console.log("已执行1");
					b=false;
					return;//停止循环
				}else {
					$("#takeFavBtn").html("加入收藏夹");
					console.log("已执行");
				}
			};
		});
	})


	//去查看评论
	$("#tocom").click(function(){
		bookId=str.substring(str.lastIndexOf("/")+1);
		userId = document.getElementById("userId").value;
		localStorage.setItem("jiajuId",bookId);
		localStorage.setItem("userId",userId);
		window.location.href = '/com/tocom';
	});

	//立即购买提交按钮
		$("#buyNowBtn").click(function(){
			let data=[];
			let quantity=$("#quantity").val();
			if(quantity==0){
				return layer.msg('至少要一件才能购买哦',{icon:5});
			}
			let price=$("#price").text();
			data.push({
				'cartId':null,
				'bookId':bookId,
				'price':parseFloat(price),
				'quantity':quantity
			});
			console.log(data);
			order_submit_popup(data);
			
		});

	//收藏夹按钮
	$("#takeFavBtn").click(function(){
		let data={
			'userId':userId,
			'jiajuId':bookId
		};
		//添加收藏夹，先判断
		if(b){
			//b为true就执行添加进入收藏夹
		$.post('/fav/insert', data, function (res) {
			if (res.code != 0) {
				return layer.msg(res.msg,{icon:2});
				$("#takeFavBtn").html("取消收藏");
				b=false;//已收藏
			}
			return layer.msg("添加成功,请前往收藏夹查看",{icon:1},function () {
				window.location.reload();//刷新
			});
		});
		}else// b为false取消收藏
		{$.getJSON('/fav/deleteFav',"jiajuId="+bookId,function (res) {
			if (res.code != 0) {
				return layer.msg(res.msg,{icon:2});
				$("#takeFavBtn").html("加入收藏夹");
				b=true;//标记为未收藏
			}
			return layer.msg("取消收藏成功！",{icon:1},function () {
				window.location.reload();//刷新
			});
		})}
	});
		
		//添加购物车提交按钮
		$("#addCartBtn").click(function(){
			let quantity=$("#quantity").val();
			if(quantity==0){
				return layer.msg('至少要购买一件才能添加购物车哦',{icon:5});
			}
			let price=$("#price").text();
			let data={
				'bookId':bookId,
				'price':parseFloat(price),
				'quantity':quantity
			};
			$.post('/cart/list', data, function (res) {
				if (res.code != 0) {
					return layer.msg(res.msg,{icon:2});
				}
				return layer.msg("添加成功,请前往购物车查看",{icon:1});
			});
		});
		
		//购买数量输入框监听
		window.check=function(obj,price){
			var qty=$(obj).val();
			if(qty<0||qty==''){
				layer.msg("输入必须大于或者等于0");
				$(obj).val(0);
				qty=0;
			}
			if(qty>10){
				layer.msg("每个用户限购10件");
				$(obj).val(10);
				qty=10;
			}
			$("#totalAccount").text("¥"+Math.floor(parseFloat(price*100 *qty))/100);
		}
});